/*
 * ParameterUtils.java
 *
 * Created on November 27, 2007, 1:27 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package hextd.util;

import java.applet.Applet;

/**
 *
 * @author hylke
 */
public class ParameterUtils {

    public static String paramToUrl(Applet source, String paramName, String deflt) {
        String param = null;
        try {
            param = source.getParameter(paramName);
        } catch (NullPointerException e) {
            System.err.println("ParameterUtils::paramToUrl: NullPointerException, used in constructor?"+e.getStackTrace());
        }
        String base_url = "";
        if (param != null) {
            if (param.indexOf("http://") == 0) {
                base_url = param;
            } else {
                base_url = source.getCodeBase().toString() + param;
            }
        } else {
            base_url = deflt;
        }
        return base_url;
    }

    public static boolean paramToBool(Applet source, String paramName, boolean deflt) {
        String param = null;
        try {
            param = source.getParameter(paramName);
        } catch (NullPointerException e) {
            System.err.println("ParameterUtils::paramToBool: NullPointerException, used in constructor?"+e.getStackTrace());
        }

        if (param != null) {
            if (param.toLowerCase().matches("(.*true.*|.*1.*|.*yes.*|.*on.*)")) {
                return true;
            } else if (param.toLowerCase().matches("(.*false.*|.*0.*|.*no.*|.*off.*)")) {
                return false;
            }
        }
        return deflt;
    }

    public static double paramToDouble(Applet source, String paramName, double deflt) {
        String param = null;
        try {
            param = source.getParameter(paramName);
        } catch (NullPointerException e) {
            System.err.println("ParameterUtils::paramToDouble: NullPointerException, used in constructor?"+e.getStackTrace());
        }
        double returnvalue;

        if (param != null && !param.equalsIgnoreCase("")) {
            try {
                Double newParam = new Double(param);
                returnvalue = newParam.doubleValue();
            } catch (NumberFormatException e) {
                returnvalue = deflt;
            }
        } else {
            returnvalue = deflt;
        }
        return returnvalue;
    }

    public static String paramToString(Applet source, String paramName, String deflt) {
        String param = null;
        try {
            param = source.getParameter(paramName);
        } catch (NullPointerException e) {
            System.err.println("ParameterUtils::paramToString: NullPointerException, used in constructor?"+e.getStackTrace());
        }
        String returnvalue;

        if (param != null && !param.equalsIgnoreCase("")) {
            returnvalue = param;
        } else {
            returnvalue = deflt;
        }
        return returnvalue;
    }

    public static int paramToInt(Applet source, String paramName, int deflt) {
        String param = null;
        try {
            param = source.getParameter(paramName);
        } catch (NullPointerException e) {
            System.err.println("ParameterUtils::paramToInt: NullPointerException, used in constructor?"+e.getStackTrace());
        }
        
        int returnvalue;
        if (param != null && !param.equalsIgnoreCase("")) {
            try {
                Integer newParam = new Integer(param);
                returnvalue = newParam.intValue();
            } catch (NumberFormatException e) {
                returnvalue = deflt;
            }
        } else {
            returnvalue = deflt;
        }
        return returnvalue;
    }
}
